﻿using System;

using BIStudio.Framework.Data;
using BIStudio.Framework.Domain;

namespace BIStudio.Framework.Log
{
    /// <summary>
    /// 日志的查询条件
    /// </summary>
    public class LogCondtiton : Entity, ICloneable
    {
        /// <summary>
        ///     内容模块
        /// </summary>
        public string EntityModule { get; set; }
        
        /// <summary>
        ///     内容类型
        /// </summary>
        public string EntityType { get; set; }

        /// <summary>
        ///     内容主键
        /// </summary>
        public int? EntityID { get; set; }

        /// <summary>
        ///     内容标题
        /// </summary>
        public string EntityTitle { get; set; }

        /// <summary>
        ///     操作类型
        /// </summary>
        public string OperateType { get; set; }

        /// <summary>
        ///     操作内容
        /// </summary>
        public string OperateContent { get; set; }

        /// <summary>
        ///     操作摘要
        /// </summary>
        public string OperateSummary { get; set; }

        /// <summary>
        ///     操作详情
        /// </summary>
        public string OperateDetail { get; set; }

        /// <summary>
        ///     用户IP
        /// </summary>
        public string UserIP { get; set; }

        /// <summary>
        ///     用户名称
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        ///     用户代码
        /// </summary>
        public int? UserID { get; set; }

        ///// <summary>
        ///// 用户时间
        ///// </summary>
        public DateTime? BeginUserTime { get; set; }
        ///// <summary>
        ///// 用户时间
        ///// </summary>
        public DateTime? EndUserTime { get; set; }

        /// <summary>
        ///     日志级别
        /// </summary>
        public int? LogLevel { get; set; }

        object ICloneable.Clone()
        {
            return MemberwiseClone();
        }

        internal DBBuilder BuildSqlCondition(DBBuilder builder)
        {
            return builder.Where()
                .Like(new { EntityModule = "%" + EntityModule + "%" })
                .And(d => d.Ge(new { CreateTime = BeginUserTime }), BeginUserTime.HasValue)
                .And(d => d.Le(new { CreateTime = EndUserTime }), EndUserTime.HasValue)
                .And(d => d.Eq(new { EntityID = EntityID }), EntityID.HasValue)
                .And(d => d.Eq(new { EntityType = EntityType }), !string.IsNullOrEmpty(EntityType))
                .And(d => d.Eq(new { OperateType = OperateType }), !string.IsNullOrEmpty(OperateType))
                .And(d => d.Eq(new { LogLevel = LogLevel }), LogLevel.HasValue)
                .And(d => d.Eq(new { UserID = UserID }), UserID.HasValue)
                .And(d => d.Like(new { EntityTitle = "%" + EntityTitle + "%" }), !string.IsNullOrEmpty(EntityTitle))
                .And(d => d.Like(new { OperateContent = "%" + OperateContent + "%" }), !string.IsNullOrEmpty(OperateContent))
                .And(d => d.Like(new { OperateSummary = "%" + OperateSummary + "%" }), !string.IsNullOrEmpty(OperateSummary))
                .And(d => d.Like(new { OperateDetail = "%" + OperateDetail + "%" }), !string.IsNullOrEmpty(OperateDetail))
                .And(d => d.Like(new { UserIP = "%" + UserIP + "%" }), !string.IsNullOrEmpty(UserIP))
                .And(d => d.Like(new { UserName = "%" + UserName + "%" }), !string.IsNullOrEmpty(UserName));
        }

        internal Func<STDLog, bool> GetCondition()
        {
            return item =>
            {
                if (BeginUserTime <= item.CreateTime)
                    return true;
                if (EndUserTime >= item.CreateTime)
                    return true;
                if (EntityID.HasValue && EntityID == item.EntityID)
                    return true;
                if (!string.IsNullOrEmpty(EntityType) && EntityType == item.EntityType)
                    return true;
                if (UserID.HasValue && UserID == item.UserID)
                    return true;
                if (LogLevel.HasValue && LogLevel == item.LogLevel)
                    return true;
                if (!string.IsNullOrEmpty(OperateType) && item.EntityModule == EntityModule)
                    return true;
                if (!string.IsNullOrEmpty(EntityModule) && item.EntityModule.Contains(EntityModule))
                    return true;
                if (!string.IsNullOrEmpty(EntityTitle) && item.EntityTitle.Contains(EntityTitle))
                    return true;
                if (!string.IsNullOrEmpty(OperateContent) && item.OperateContent.Contains(OperateContent))
                    return true;
                if (!string.IsNullOrEmpty(OperateSummary) && item.OperateSummary.Contains(OperateSummary))
                    return true;
                if (!string.IsNullOrEmpty(OperateDetail) && item.OperateDetail.Contains(OperateDetail))
                    return true;
                if (!string.IsNullOrEmpty(UserIP) && item.UserIP.Contains(UserIP))
                    return true;
                if (!string.IsNullOrEmpty(UserName) && item.UserName.Contains(UserName))
                    return true;
                return false;
            };
        }
    }
}